package com.qf.web.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qf.aop.aspect.ApiOperationLog;
import com.qf.aop.aspect.ApiOperationLogTitle;
import com.qf.api.blog.dto.CatalogArticleDTO;
import com.qf.api.blog.dto.CatalogDTO;
import com.qf.api.blog.dto.CatalogSortDTO;
import com.qf.api.blog.entity.WikiCatalogEntity;
import com.qf.api.blog.service.WikiCatalogService;
import com.qf.core.enums.common.BusinessTypeEnum;
import com.qf.core.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sin
 * @Date: 2025/5/25 20:40
 * @Description:
 **/
@RestController
@RequestMapping("/admin/catalog")
@ApiOperationLogTitle("知识库目录模块")
public class AdminWikiCatalogController {

    @Resource
    private WikiCatalogService wikiCatalogService;

    @Operation(summary = "新增知识库目录")
    @ApiOperationLog(desc = "新增知识库目录",businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseResult<Boolean> addWikiCatalog(@RequestBody @Valid CatalogDTO catalogDTO) {
        return ResponseResult.success(wikiCatalogService.
                addWikiCatalog(
                        catalogDTO.getWikiId(),
                        catalogDTO.getTitle(),
                        catalogDTO.getIcon())
        );
    }

    @Operation(summary = "修改目录顺序")
    @ApiOperationLog(desc = "修改目录顺序",businessType = BusinessTypeEnum.MODIFY)
    @PutMapping
    public ResponseResult<Boolean> editCatalogSort(@RequestBody @Validated List<@Valid CatalogSortDTO> catalogSortDTOS) {
        wikiCatalogService.editCatalogSort(catalogSortDTOS);
        return ResponseResult.success(true);
    }

    @Operation(summary = "获取知识库目录详情",description = "根据Id获取知识库目录详情")
    @GetMapping("/{id}")
    public ResponseResult<CatalogDTO> getCatalog(@PathVariable("id") @NotNull Long id) {
        WikiCatalogEntity catalog = wikiCatalogService.getById(id);
        return ResponseResult.success(BeanUtil.copyProperties(catalog, CatalogDTO.class));
    }
    @Operation(summary = "修改知识库目录",description = "根据知识库Id修改知识库目录信息")
    @ApiOperationLog(desc = "修改知识库目录",businessType = BusinessTypeEnum.MODIFY)
    @PutMapping("/{id}")
    public ResponseResult<Boolean> updateWikiCatalog(@PathVariable("id") @NotNull Long id ,@RequestBody @Validated CatalogDTO catalogDTO) {
        return ResponseResult.success(wikiCatalogService.updateWikiCatalog(id,catalogDTO));
    }

    @Operation(summary = "删除知识库目录",description = "根据知识库Id删除知识库目录")
    @ApiOperationLog(desc = "删除知识库目录",businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> removeWikiCatalog(@PathVariable("id") @NotNull Long id) {
        return ResponseResult.success(wikiCatalogService.removeWikiCatalog(id));
    }

    @Operation(summary = "添加文章",description = "根据知识库Id添加文章")
    @ApiOperationLog(desc = "添加文章",businessType = BusinessTypeEnum.ADD)
    @PostMapping("/{id}/article")
    public ResponseResult<String> addArticle(@PathVariable("id") @NotNull Long parentId,@RequestBody List<CatalogArticleDTO> articleDTOS) {
        String s = wikiCatalogService.addArticle(parentId, articleDTOS);
        return ResponseResult.success(s);
    }
}
